<html>
<head><meta charset="utf-8"><title>Dangling pointers in enum discriminants · t-compiler/const-eval · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/index.html">t-compiler/const-eval</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Dangling.20pointers.20in.20enum.20discriminants.html">Dangling pointers in enum discriminants</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="147515706"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Dangling%20pointers%20in%20enum%20discriminants/near/147515706" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Dangling.20pointers.20in.20enum.20discriminants.html#147515706">(Nov 12 2018 at 09:54)</a>:</h4>
<p><span class="user-mention" data-user-id="124288">@Oli</span> So I wrote <a href="https://github.com/RalfJung/rust/commit/71badfdc94586c66f0c5bb570401a74b356c06e7" target="_blank" title="https://github.com/RalfJung/rust/commit/71badfdc94586c66f0c5bb570401a74b356c06e7">https://github.com/RalfJung/rust/commit/71badfdc94586c66f0c5bb570401a74b356c06e7</a> because I realized (a) these assertions in <code>read_discriminant</code> can actually be violated (I have an example of an ICE here), and (b) this is yet another place where we made the wrong assumption that pointer values are never 0. Only in-bounds pointers are never 0. Unfortunately, fixing the latter leads to a build error in libstd...</p>
<div class="codehilite"><pre><span></span>error[E0080]: it is undefined behavior to use this value============&gt;  ] 28/29: std
   --&gt; libstd/sys_common/thread_local.rs:254:5
    |
254 |     static DTORS: StaticKey = StaticKey::new(Some(run_dtors));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer at .dtor, but expected a valid enum discriminant
    |
    = note: The rules on what exactly is undefined behavior aren&#39;t clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
</pre></div>



<a name="147515734"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Dangling%20pointers%20in%20enum%20discriminants/near/147515734" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Dangling.20pointers.20in.20enum.20discriminants.html#147515734">(Nov 12 2018 at 09:55)</a>:</h4>
<p>ah, this is a pointer to a function</p>



<a name="147515740"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Dangling%20pointers%20in%20enum%20discriminants/near/147515740" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Dangling.20pointers.20in.20enum.20discriminants.html#147515740">(Nov 12 2018 at 09:55)</a>:</h4>
<p><code>check_bounds_ptr</code> should probably accept those at offset 0</p>



<a name="147516906"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Dangling%20pointers%20in%20enum%20discriminants/near/147516906" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Dangling.20pointers.20in.20enum.20discriminants.html#147516906">(Nov 12 2018 at 10:23)</a>:</h4>
<p>Submitted as <a href="https://github.com/rust-lang/rust/pull/55894" target="_blank" title="https://github.com/rust-lang/rust/pull/55894">https://github.com/rust-lang/rust/pull/55894</a></p>



<a name="147517279"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Dangling%20pointers%20in%20enum%20discriminants/near/147517279" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Dangling.20pointers.20in.20enum.20discriminants.html#147517279">(Nov 12 2018 at 10:31)</a>:</h4>
<p>looks good to me</p>



<a name="147517344"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Dangling%20pointers%20in%20enum%20discriminants/near/147517344" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Dangling.20pointers.20in.20enum.20discriminants.html#147517344">(Nov 12 2018 at 10:32)</a>:</h4>
<p>no need to address the missing test in this PR, so r=me with or without the test</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>